/**
    Copyright (C) 2012 Guido Ludwig

    This file is part of JATT.
	http://code.google.com/p/jatt/

    JATT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.guidoludwig.jatt.chain.commands;

import java.util.ArrayList;
import java.util.List;

import de.guidoludwig.jatt.JATT;
import de.guidoludwig.jatt.chain.JATTContext;
import de.guidoludwig.jatt.chain.commands.config.ClearConfig;
import de.guidoludwig.jatt.util.AudioInfo;
import de.guidoludwig.jatt.util.ImageInfo;

/**
 * @author Guido Ludwig
 */
public class ClearCommand extends JATTCommand<ClearConfig> {



	@Override
	protected boolean execute0(JATTContext context) {
		List<AudioInfo> toRemove = new ArrayList<AudioInfo>();
		if (getConfig().isRemoveArtworkTags()) {
			for (AudioInfo info : context.getTagged()) {
				try {
					info.deleteArtwork();
					toRemove.add(info);
					JATT.debug("Remove Tag from "
							+ info.getFile().getAbsolutePath());
				} catch (Exception e) {
					JATT.error(e);
				}
			}
		}
		for (AudioInfo audioInfo : toRemove) {
			context.moveToUntagged(audioInfo);
		}
		if (getConfig().isRemoveFolderImage() && context.hasFolderImage()) {
			JATT.debug("Deleted Folder Image: " + context.getFolderImageFile().getAbsolutePath());
			context.getFolderImageFile().delete();
			context.setFolderImage(null);
			context.setFolderImage(null);
		}
		if (getConfig().isRemoveAlternativeArtwork()) {
			List<ImageInfo> removed = new ArrayList<>();
			for (ImageInfo info : context.getAlternativeImages()) {
				info.getFile().delete();
				JATT.debug("Deleted Folder Image: " + info.getFile().getAbsolutePath());
				removed.add(info);
			}
			for (ImageInfo imageInfo : removed) {
				context.removeFromAlternativeImages(imageInfo);
			}
		}
		return false;
	}

}
